 /********************************************************************************************\
 * libeds - extensible data structures library                                                *
 * Copyright (C) 2006, Dan Kruchinin, <just.asgard@gmail.com>                                 *
 *                                                                                            *
 * -> test/tree/test_bin_tree.c                                                               *
 *                                                                                            *
 * This library is free software; you can redistribute it and/or modify it under the terms of *
 * the GNU Lesser General Public License as published by the Free Software Foundation;        *
 * either version 2.1 of the License, or (at your option) any later version.                  *
 *                                                                                            *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;  *
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  *
 * See the GNU Lesser General Public License for more details.                                *
 *                                                                                            *
 * You should have received a copy of the GNU Lesser General Public License along with this   *
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,  *
 * Boston, MA 02111-1307 USA                                                                  *
 \********************************************************************************************/

#include <ncurses.h>
#include <stdlib.h>
#include "trees/bin_tree.h"
#include "test/test_base.h"
#include "test/test_bin_tree.h"

static void __dump_tree(bin_tree*, bit_node*, int, int, void (*f)(bit_node*));

void dump_tree(bin_tree* t, void (*print)(bit_node*))
{
  int x, y;

  getmaxyx(stdscr, y, x);
  x /= 2;
  __dump_tree(t, t->root(t->tree), x, TREE_DUMP_START_LINE, print);
}

void dump_tree_info(bin_tree* t, void (*extra)(bin_tree*))
{
  int x, y;

  getmaxyx(stdscr, y, x);
  move(y - (TREE_INFO_LINES_MAX + 1), 0);
  test_printf("tree height: %d\n", t->node_height(t->root(t->tree)));
  test_printf("tree internal path len: %d\n", t->node_epl(t->root(t->tree)));
  test_printf("tree external path len: %d\n", t->node_epl(t->root(t->tree)));

  if(extra != NULL)
	extra(t);
}

static void __dump_tree(bin_tree* t, bit_node* n, int x, int y, void (*print)(bit_node*))
{
  int a = 3, b = 2;
  
  if((x < 0) || (y < 0))
	return;

  //  getyx(stdscr, y, x);
  move(y, x);
  
  if(node_empty(n)) {
	test_printf("[-]");
	return;
  }

  print(n);
  
  if(!node_empty(t->node_left(n)))
	a += t->node_subnodes(t->node_left(n));
  if(!node_empty(t->node_right(n)))
	b += t->node_subnodes(t->node_right(n));
  
  move(0, 8);
  test_printf("[%d]: %d, %d", x, a, b);
  x += TREE_FIELD_MAX / 2;
  
  move(y + 1, x - 1);
  test_printf("/");
  __dump_tree(t, t->node_left(n), x - a - TREE_FIELD_MAX, y + 2, print);
  move(y + 1, x + 1);
  test_printf("\\");
  __dump_tree(t, t->node_right(n), x + b + TREE_FIELD_MAX, y + 2, print);
}
